package com.ysl.study.opensource.spring.profile.service.impl;

import java.util.function.Predicate;
import java.util.stream.LongStream;
import java.util.stream.Stream;

/**
 * TODO
 *
 * @author 许棒棒
 * @date 2021/07/18 16:08:33
 */
public class Test {


    public static void main(String[] args) {


        Stream<Long> boxed = LongStream.range(1, 100).boxed();
        Long aLong = boxed.filter(e -> e.intValue() == 20).findFirst().orElse(0L);
        System.out.println(aLong);

        boxed = LongStream.range(1, 100).boxed();
        Long aLong1 = filterAndGet(boxed, e -> e.intValue() == 30, 0L);

        boxed = LongStream.range(1, 100).boxed();
        Long aLong2 = filterAndGet(boxed, e -> e.intValue() == 3000, -1L);
        System.out.println(aLong1);
        System.out.println(aLong2);


        boxed = LongStream.range(1, 100).boxed();
        System.out.println(getValue(boxed, 30L, 0L));
        boxed = LongStream.range(1, 100).boxed();
        System.out.println(getValue(boxed, 3000L, -1L));


    }

    private static Long getValue(Stream<Long> stream, Long predicateValue, Long defaultValue) {
        return filterAndGet(stream, e -> e.intValue() == predicateValue.intValue(), defaultValue);
    }

    private static <T> T filterAndGet(Stream<T> stream, Predicate<T> predicate, T defaultValue) {
        return stream.filter(predicate).findFirst().orElse(defaultValue);
    }

}
